<template>
  <div>
    <c-table-box ref="cTableBox" checkbox hide-func :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <c-permission v-if="type==='all'" code="all">
          <ActiveCheckTask :sel-data="selections" opt-code="activate" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="ReportExport" @click="exportVisible=true">导出</c-button>
          <c-button type="primary" opt-code="hardReport" @click="handleCreateReport">生成验收报告</c-button>
          <c-button type="primary" opt-code="changeAcceptPerson" @click="handleChangeAcceptPerson">变更验收人员</c-button>

        </c-permission>
        <c-permission v-else-if="type==='waitApply'" code="application">
          <c-button type="primary" opt-code="application" @click="clickToRouterHandle('CheckHardApply', '报验')">报验</c-button>
          <c-button type="primary" opt-code="merge" @click="clickToRouterMergeHandle('CheckHardMerge')">合并验收</c-button>
        </c-permission>
        <c-permission v-else-if="type==='audit'" code="audit">
          <Audit :sel-data="selections" opt-code="audit" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='planCheck'" code="arrangeapplication">
          <PlanCheck :sel-data="selections" opt-code="arrangeapplication" @on-success="refreshTableData" />
          <Exception :sel-data="selections" opt-code="exception" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='waitCheck'" code="check">
          <c-button type="primary" opt-code="execute" @click="clickToRouterExecuteHandle()">验收执行</c-button>
          <c-button type="primary" opt-code="noCheckArea" @click="clickNoCheckArea()">非本次验收面积维护</c-button>
        </c-permission>
        <c-permission v-else-if="type==='verify'" code="verify">
          <c-button type="primary" opt-code="verify" @click="clickToRouterHandle('CheckHardVerify', '核对结案类型')">核对结案类型</c-button>
        </c-permission>
      </template>
    </c-table-box>
    <export-files
      v-if="exportVisible"
      title="导出"
      :config="exportConfig"
      :visible.sync="exportVisible"
      @success="handleExportSuccess"
    />
    <person-dlg
      v-if="showAcceptorDlg"
      ref="designerDlg"
      :visible.sync="showAcceptorDlg"
      :detail-data="activedData"
      @success="refreshTableData"
    />

  </div>
</template>
<script>
import personDlg from '../../components/acceptPersonDlg'
import { otherFiltersItem } from '@/utils'
import ActiveCheckTask from '../../components/activeCheckTask'
import Audit from '../../components/audit'
import Exception from '../../components/exception'
import PlanCheck from '../../components/planCheck'
export default {
  name: 'List',
  components: {
    ActiveCheckTask,
    Audit,
    PlanCheck,
    Exception,
    personDlg
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    },
    activeType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showAcceptorDlg: false,
      activedData: {},
      exportVisible: false,
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/shopAccept/selectPage',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-perCode': 'checkhard',
          'Permission-App': 'slm'
        },
        setting: []
      },
      allSetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        // {
        //   prop: 'expectAcceptApplyReportTim',
        //   label: '预计验收申请提报时间'
        // },
        // {
        //   prop: 'isOutTimeUnaccept',
        //   label: '是否超期未报验',
        //   propEnum: {
        //     appCode: 'slm',
        //     domainCode: 'marketing',
        //     dictTypeCode: 'BOOLEANS'
        //   }
        // },
        {
          prop: 'acceptEndType',
          label: '结案类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_CLOSED_TYPE'
          }
        },
        {
          prop: 'shopName',
          width: 100,
          label: '所属门店'
        },
        {
          prop: 'clientName',
          width: 100,
          label: '所属客户'
        },
        {
          prop: 'officeName',
          width: 120,
          label: '所属办事处'
        },
        {
          prop: 'range',
          width: 100,
          label: '装修范围',
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          width: 100,
          label: '施工单位'
        },
        {
          prop: 'principal',
          width: 120,
          label: '施工负责人'
        },
        {
          prop: 'proSupervisorName',
          width: 100,
          label: '工程监理'
        },
        {
          prop: 'formSupervisorName',
          width: 100,
          label: '形象督导'
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      waitApplySetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        // {
        //   prop: 'expectAcceptApplyReportTim',
        //   label: '预计验收申请提报时间'
        // },
        // {
        //   prop: 'isOutTimeUnaccept',
        //   label: '是否超期未报验',
        //   propEnum: {
        //     appCode: 'slm',
        //     domainCode: 'marketing',
        //     dictTypeCode: 'BOOLEANS'
        //   }
        // },
        {
          prop: 'shopName',
          width: 100,
          label: '所属门店'
        },
        {
          prop: 'clientName',
          width: 100,
          label: '所属客户'
        },
        {
          prop: 'officeName',
          width: 120,
          label: '所属办事处'
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 100,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          width: 100,
          label: '施工单位'
        },
        {
          prop: 'principal',
          width: 120,
          label: '施工负责人'
        },
        {
          prop: 'proSupervisorName',
          width: 100,
          label: '工程监理'
        },
        {
          prop: 'formSupervisorName',
          width: 100,
          label: '形象督导'
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      otherSetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 100,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        {
          prop: 'shopName',
          width: 100,
          label: '所属门店'
        },
        {
          prop: 'clientName',
          width: 100,
          label: '所属客户'
        },
        {
          prop: 'officeName',
          width: 120,
          label: '所属办事处'
        },
        {
          prop: 'range',
          width: 100,
          label: '装修范围',
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          width: 100,
          label: '施工单位'
        },
        {
          prop: 'principal',
          width: 120,
          label: '施工负责人'
        },
        {
          prop: 'proSupervisorName',
          width: 100,
          label: '工程监理'
        },
        {
          prop: 'formSupervisorName',
          width: 100,
          label: '形象督导'
        },
        {
          prop: 'marketType',
          width: 100,
          label: '报图类型',
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      typeFilters: [],
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    exportConfig() {
      return [{
        code: 'export',
        name: '硬装验收管理',
        dataApi: '/slm-admin/api/v1/shopAccept/exportShopInfo',
        selectedRows: this.selections,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id',
        headers: {
          'Permission-perCode': 'checkhard',
          'Permission-App': 'slm'
        }
      }]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
        this.setTableSetting(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.refreshTableData()
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    handleCreateReport() {
      if (!this.singleJudge(this.selections, '报告生成', true)) {
        return
      }
      const routeUrl = this.$router.resolve({
        path: '/printReport',
        query: { id: this.selections[0].id, type: this.selections[0].acceptType || 'HARD' }
      })
      window.open(routeUrl.href, '_blank')
    },
    handleChangeAcceptPerson() {
      if (!this.singleJudge(this.selections, '变更', true)) {
        return
      }
      this.activedData = this.selections[0]
      this.showAcceptorDlg = true
    },
    // 导出成功
    handleExportSuccess() {
      this.$message.success('导出成功！')
    },
    // 设置table setting
    setTableSetting(v) {
      switch (v) {
        case 'all':
          this.$set(this.tableConfig, 'setting', this.allSetting)
          break
        case 'waitApply':
          this.$set(this.tableConfig, 'setting', this.waitApplySetting)
          break
        default:
          this.$set(this.tableConfig, 'setting', this.otherSetting)
          break
      }
    },
    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'waitApply':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'I1', '=')
          ]
          break
        case 'audit':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'I2', '='),
            otherFiltersItem('isAccept', '1', '=')
          ]
          break
        case 'planCheck':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'J3', '=')
          ]
          break
        case 'waitCheck':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', '[J1]', 'in')
          ]
          break
        case 'verify':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '<>'),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'L2', '=')
          ]
          break
      }
    },
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    selectionChange(v) {
      this.selections = v
    },
    // 删除
    clickRemoveHandle() {
      this.$confirm(`是否确认删除勾选的${this.selections.length}条数据？`, {
        type: 'warning',
        title: '删除'
      }).then(() => {
        console.log('删除成功！')
      })
    },
    // 跳转
    clickToRouterHandle(name, warnText) {
      if (!this.singleJudge(this.selections, warnText || '操作', true)) {
        return
      }
      const s = this.selections[0]
      this.$router.push({ name, query: { id: s.id, acceptType: s.acceptType, officeCode: s.officeCode, shopCode: s.shopCode, clientCode: s.clientCode }})
    },
    // 合并验收
    clickToRouterMergeHandle(name) {
      if (this.selections.length <= 1) {
        this.$message.warning('请选择多条数据！')
        return
      }
      const constCodes = new Set()
      for (const item of this.selections) {
        constCodes.add(item.constCode)
      }
      if (constCodes.size > 1) {
        this.$message.warning('只有相同门店建设项目号下的数据可以合并验收！')
        return
      }
      this.$router.push({ name, query: { ids: this.selections.map(e => e.id).join(','), shopCode: this.selections[0].shopCode }})
    },
    // 非本次验收面积维护
    clickNoCheckArea() {
      if (this.selections.length !== 1) {
        this.$message.warning('请选择一条数据！')
        return
      }
      const s = this.selections[0]
      // 门头也要第一步
      const name = 'CheckHardExecute'
      let hasStores = 'no'
      if ((s.range || '').includes('STORES')) {
        hasStores = 'yes'
      }
      this.$router.push({ name, query: { isOnlyCheckArea: true, id: s.id, decorationTaskCode: s.decorationTaskCode, acceptTaskCode: s.acceptTaskCode, constUnitCode: s.constUnitCode, hasStores }})
    },
    // 执行
    clickToRouterExecuteHandle() {
      if (this.selections.length !== 1) {
        this.$message.warning('请选择一条数据！')
        return
      }
      const s = this.selections[0]
      // 门头也要第一步
      const name = 'CheckHardExecute'
      let hasStores = 'no'
      if ((s.range || '').includes('STORES')) {
        hasStores = 'yes'
      }
      this.$router.push({ name, query: { id: s.id, decorationTaskCode: s.decorationTaskCode, acceptTaskCode: s.acceptTaskCode, constUnitCode: s.constUnitCode, hasStores }})
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: 'CheckHardDetail', query: { id: row.id, taskCode: row.decorationTaskCode, unitCode: row.constUnitCode }})
    }
  }
}
</script>
